package com.nipun.facet.neural;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.nipun.neural.engine.runners.SimpleNetworkRunner;
import com.nipun.neural.objects.Network;
import com.nipun.neural.objects.Neuron;


public class FacetnetworkRunnerImpl extends SimpleNetworkRunner implements FacetNetworkRunner, Serializable {
	
	private double[][] inputMap;
	private boolean output;
	private boolean isLearningOn;
	private double threshHold;
	private int xDim;
	private int ydim;
	
	private Neuron[][] inputNeurons;
	
	public FacetnetworkRunnerImpl(Network net, int x, int y){
		super(net);
		if(net.getInputNodes().length != x*y){
			System.out.println("input map has different size than intended network....exiting");
			System.exit(-1);
		}
		if(net.getOutputNodes().length > 1){
			System.out.println("not a valid network because it has more than one outputnodes...exiting");
			System.exit(-1);
		}
		inputMap = new double[x][y];
		output = false;
		isLearningOn = true;
		inputNeurons = new Neuron[x][y];
		Object[] netInputNeurons = net.getInputNodes();
		int count=0;
		for(int i=0; i<x; i++){
			for(int j=0; j<y; j++){
				inputNeurons[i][j] = (Neuron)netInputNeurons[count];
			}
		}
		threshHold = 0.75;
		xDim = x;
		ydim = y;
	}

	
	public void backPropagate(boolean value) {
		double out = value?1:0;
		List l = new ArrayList();
		l.add(new Double(out));
		super.backPropagate(l);
	}

	
	public boolean run(double[][] image) {
		if(image.length != inputMap.length){
			System.out.println("wrong input dimensions....ignoring");
			return false;
		}
		List l = new ArrayList();
		for(int i=0; i<xDim; i++){
			for(int j=0; j<ydim; j++){
				l.add(new Double(image[i][j]));
			}
		}
		double result = (Double)super.run(l).get(0);
		if(result > threshHold){
			return true;
		}
		return false;
	}

	public boolean isLearningOn() {
		return isLearningOn;
	}

	public void setLearningOn(boolean isLearningOn) {
		this.isLearningOn = isLearningOn;
	}

	public void setThreshHold(double threshHold) {
		this.threshHold = threshHold;
	}

	
	public int getHeight() {
		return xDim;
	}

	
	public int getWidth() {
		return ydim;
	}

}
